package com.java.springboot.elasticsearch.controller;

import com.java.springboot.elasticsearch.common.CommonResult;
import com.java.springboot.elasticsearch.param.BookSearchParam;
import com.java.springboot.elasticsearch.service.ElasticSearchService;
import com.java.springboot.elasticsearch.service.ElasticSearchServiceTmp;
import com.java.springboot.elasticsearch.vo.BookSearchResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 搜索
 *
 * @author zhangtengfei
 * @date 2021/10/11 17:18
 */
@RestController
@RequestMapping("/es/book/search")
public class EsBookSearchController {

    @Resource
    private ElasticSearchService elasticSearchService;

    @Resource
    private ElasticSearchServiceTmp elasticSearchServiceTmp;

    /**
     * @param index "book"
     * @return
     */
    @GetMapping("/rest")
    public CommonResult<List<BookSearchResultVO>> searchByRest(@RequestParam String index) {
        BookSearchParam bookSearchParam = new BookSearchParam();
        bookSearchParam.setDescription("童话故事");
        bookSearchParam.setHighlightedField("description");
        List<BookSearchResultVO> books = elasticSearchService.queryByRest(index, bookSearchParam);
        return CommonResult.success(books);
    }

    @GetMapping("/rest/tmp")
    public CommonResult<List<String>> searchByRestTmp(@RequestParam String index,
                                                      @RequestParam Long startTime,
                                                      @RequestParam Long endTime) {
        List<String> list = elasticSearchServiceTmp.queryByRest(index, startTime, endTime);
        return CommonResult.success(list);
    }
}
